package com.wxmit.starter.xfile.config;


import cn.hutool.core.collection.ListUtil;
import com.wxmit.base.constant.ShushanConstants;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.List;
import java.util.Objects;

/**
 *  文件配置类
 * @author huangxiong
 * @date 2024/03/20 10:09
 */
public class ShushanFileConfig {

    /**
     *  文件前缀
     */
    public static String domain = "http://localhost:8080/profiles";

    /**
     *  磁盘存储地址
     */
    public static String path = "/home/ss";


    /**
     *  文件分隔符
     */
    public static String separator = File.separator;

    /**
     *  文件名最大大小
     */
    public static int fileNameMaxLength = 100;

    /**
     *  文件默认大小 （50M）
     */
    public static long fileMaxSize = 50 * 1024 * 1024;

    public static List<String> allowedExtension = ListUtil.of(
            // 图片
            "bmp", "gif", "jpg", "jpeg", "png","webp",
            // flash
            "swf", "flv",
            // word excel powerpoint
            "doc", "docx", "xls", "xlsx", "ppt", "pptx", "html", "htm", "txt",
            // 压缩文件
            "rar", "zip", "gz", "bz2",
            // 视频格式
            "swf", "flv", "mp3", "wav", "wma", "wmv", "mid", "avi", "mpg","asf", "rm","mp4", "avi", "rmvb",
            // pdf
            "pdf"
    );

    /**
     *  删除文件域名前缀
     * @param val 文件地址
     * @return 相对的文件路径
     */
    public static String delFilePrefix(String val){
        if(Objects.nonNull(val) &&  val.startsWith(domain)){
            return val.replaceFirst(domain ,"");
        }
        return val;
    }


    /**
     *  装饰文件域名前缀
     * @param val 文件地址
     * @return 用户可访问的文件路径
     */
    public static String decorateFilePrefix(String val){
        if(Objects.nonNull(val) && !val.startsWith(ShushanConstants.HTTP) && !val.startsWith(ShushanConstants.HTTPS)){
            return ShushanFileConfig.domain + val;
        }
        return val;
    }


    /**
     *  添加文件路径
     * @param tailPath 文件路径
     * @return 文件路径
     */
    public static String plusPath(String tailPath){
        return tailPath + File.separator;
    }
}
